<template>
  <div class="house-search">
    <!-- 返回按钮 -->
    <div class="back-btn" @click="goBack">
      <span>返回首页</span>
    </div>

    <!-- 搜索区域 -->
    <div class="search-container">
      <el-input
        v-model="searchQuery"
        placeholder="输入城市、区域或小区名称..."
        class="search-input"
        @keyup.enter="handleSearch"
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleSearch" :loading="loading">
        搜索
      </el-button>
    </div>

    <!-- 搜索结果区域 -->
    <div class="search-results" v-if="searchResults.length > 0">
      <div v-for="house in searchResults" 
           :key="house.id" 
           class="house-card"
           @click="goToDetail(house.id)">
        <!-- 房源信息头部 -->
        <div class="house-header">
          <div class="house-info-grid">
            <!-- 第一行 -->
            <div class="info-item">
              <div class="info-label">
                <el-icon><House /></el-icon>
                房型
              </div>
              <div class="info-value">一室一厅</div>
            </div>
            <div class="info-item">
              <div class="info-label">
                <el-icon><Expand /></el-icon>
                面积
              </div>
              <div class="info-value">{{ house.area }}㎡</div>
            </div>
          </div>
        </div>

        <!-- 其他房源信息 -->
        <div class="house-content">
          <div class="title">{{ house.title }}</div>
          <div class="address">
            <el-icon><Location /></el-icon>
            {{ house.address }}
          </div>
          <div class="price-info">
            <span class="price">¥{{ house.price }}/月</span>
            <span class="deposit">押金：¥{{ house.deposit }}</span>
          </div>
          <div class="facilities">
            <span v-for="facility in parseFacilities(house.facilities)" 
                  :key="facility"
                  class="facility-tag">
              {{ facility }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination" v-if="total > 0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Search, Location, House, Expand } from '@element-plus/icons-vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const searchQuery = ref('')
const searchResults = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 返回首页
const goBack = () => {
  router.push('/')
}

// 跳转到详情页
const goToDetail = (id) => {
  router.push({
    path: '/house-detail',
    query: { id: id }
  })
}

// 处理搜索
const handleSearch = async () => {
  if (!searchQuery.value.trim()) {
    ElMessage.warning('请输入搜索关键词')
    return
  }

  loading.value = true
  try {
    const params = {
      text: searchQuery.value,
      page: currentPage.value,
      page_size: pageSize.value
    }

    const response = await axios.get('http://127.0.0.1:8000/users/search/', { params })
    searchResults.value = response.data.results
    total.value = response.data.count
  } catch (error) {
    console.error('搜索出错:', error)
    ElMessage.error('搜索失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 格式化朝向
const formatOrientation = (orientation) => {
  const orientationMap = {
    'south': '朝南',
    'north': '朝北',
    'east': '朝东',
    'west': '朝西'
  }
  return orientationMap[orientation] || orientation
}

// 解析设施字符串为数组
const parseFacilities = (facilities) => {
  try {
    return JSON.parse(facilities.replace(/'/g, '"'))
  } catch (e) {
    return []
  }
}

// 从路由参数获取搜索关键词
onMounted(() => {
  const searchText = route.query.text
  if (searchText) {
    searchQuery.value = searchText
    handleSearch()
  }
})
</script>

<style scoped>
.house-search {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.search-container {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.search-input {
  flex: 1;
}

.house-card {
  margin-bottom: 20px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
}

.house-card:hover {
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  transform: translateY(-2px);
}

.house-header {
  padding: 15px 20px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}

.house-info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.info-label {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #909399;
  font-size: 14px;
  margin-bottom: 5px;
}

.info-value {
  font-size: 16px;
  color: #303133;
  font-weight: 500;
}

.house-content {
  padding: 20px;
}

.title {
  font-size: 18px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: bold;
}

.address {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #606266;
  margin-bottom: 10px;
}

.price-info {
  margin: 10px 0;
}

.price {
  font-size: 20px;
  color: #ff4d4f;
  font-weight: bold;
  margin-right: 15px;
}

.deposit {
  color: #909399;
  font-size: 14px;
}

.facilities {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.facility-tag {
  padding: 2px 8px;
  background-color: #f0f2f5;
  color: #606266;
  border-radius: 4px;
  font-size: 12px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/* 添加图标样式 */
.el-icon {
  vertical-align: middle;
}
</style> 